<?php
require_once('imarty.php');
require_once('lib/class.gifencoder.php');

class animarty{
var $frame;
var $h;
var $w;
var $loop;
var $disposal;
var	$trans_red;
var	$trans_blue;
var	$trans_green;
var $cache;

function animarty($file){
	if($file!=''){
		$this->load_xml($file);
	}
}
function load_xml($xml, $string=false){
	if(!$string){
		$xml = simplexml_load_file($xml);
	}else{
		$xml = simplexml_load_string($xml);
	}
	$this->frame = $xml->frame;
	$this->h = (string) $xml['h'];
	$this->w = (string) $xml['w'];
	$this->loop = (int) $xml['loop'];
	$this->disposal = (int) $xml['disposal'];
	$this->trans_red = (int) $xml['trans_red'];
	$this->trans_blue = (int) $xml['trans_blue'];
	$this->trans_green = (int) $xml['trans_green'];
	$this->cache = (string) $xml['cache'];
}

function render(){
	$i = 0;
	$count = count($this->frame);
	while($i < $count){
		$img_frame[$i] = $this->render_frame($i);
		if($this->frame[$i]['duration']){
			$duration[$i] = (int) $this->frame[$i]['duration'];
		}else{
			$duration[$i] = 10;
		}
		++$i;
	}
	$gif = new GifEncoder($img_frame, $duration, $this->loop, $this->disposal, $this->trans_red, $this->trans_blue, $this->trans_green, 'bin');
	$this->image = $gif->GetAnimation();
}	
function render_frame($n){
	$frame = $this->frame[$n];
	if($frame['src']){
		if(function_exists('exif_imagetype')){
			$source = exif_imagetype($frame['src']);
		}else{
			$source = getimagesize($frame['src']);
			$source = $source[2];
		}
		ob_start();
		switch($source){
			case 1:
			   $image = imagecreatefromgif($frame['src']);
			break;
			case 2:
			   $image = imagecreatefromjpeg($frame['src']);
			break;
			case 3:
			   $image = imagecreatefrompng($frame['src']);
			break;
			case 5:
			   $image = imagecreatefrompsd($frame['src']);
			break;
			case 15:
			   $image = imagecreatefromwbmp($frame['src']);
			break;
			case 16:
			   $image = imagecreatefromxbm($frame['src']);
			break;
			default:
				$image = imagecreatetruecolor(1,1);
			break;
			}
			$cool = imagecreatetruecolor($this->w, $this->h);
			imagecopy ($cool, $image, 0, 0, 0, 0, $this->w, $this->h);
			imagegif($cool);
			$image = ob_get_contents();
			ob_clean();
	}
	$children = $frame->children();
	$count = count($children);
	$z = 0;
	while($z < $count){
		$element = dom_import_simplexml($children[$z]);
		$name = $element->nodeName;
		if($name=='imarty'){
			if(isset($children[$z]['src'])){
				$imarty = new imarty($children[$z]['src']);
			}else{
				$imarty = new imarty;
				$imarty->simple_xml_input($children[$z]);
			}
			$imarty->render();
			$image = $imarty->return_image('gif');
		}elseif($name=='img'){
			$imagetype = getimagesize($children[$z]['src']);
			$imagetype = $imagetype[2];
			switch($imagetype){
			case 1:
			   $image = imagecreatefromgif($children[$z]['src']);
			break;
			case 2:
			   $image = imagecreatefromjpeg($children[$z]['src']);
			break;
			case 3:
			   $image = imagecreatefrompng($children[$z]['src']);
			break;
			case 5:
			   $image = imagecreatefrompsd($children[$z]['src']);
			break;
			case 15:
			   $image = imagecreatefromwbmp($children[$z]['src']);
			break;
			case 16:
			   $image = imagecreatefromxbm($children[$z]['src']);
			break;
			default:
				$image = imagecreatetruecolor(1,1);
			break;
			}
			//imagetruecolortopalette ($image,0, 255);
			ob_start();
			imagegif($image);
			imagedestroy($image);
			$image = ob_get_contents();
			ob_clean();
		}
		++$z;
	}
	return $image;
}

function output($filename=''){
	if($filename === ''){
		header("Content-type: image/gif");
		echo $this->image;
	}else{
		$res = fopen($filename, 'wb');
		fwrite($res,$filename);
		fclose($res);
	}
	}
}

?>